{{- define "alert-manager-spec" }}
{{- $context := index . 0 }}
{{- $spec := index . 1 }}
- scheme: {{ $spec.scheme }}
  static_configs:
    - targets:
        - {{ $spec.target }}
  path_prefix: {{ $spec.path }}
  {{- if $spec.basicAuth }}
  basic_auth:
    username: {{ $spec.basicAuth.username }}
    password: {{ $spec.basicAuth.password }}
  {{- end }}
  {{- if $spec.bearerToken }}
  authorization:
    credentials: {{ $spec.bearerToken }}
  {{- end }}
  {{- if $spec.tlsConfig }}
  tls_config:
    {{- if $spec.tlsConfig.ca }}
    ca_file: /etc/prometheus/secrets/alertmanagers-tls-config/{{$spec.name}}_ca
    {{- end }}
    {{- if $spec.tlsConfig.cert }}
    cert_file: /etc/prometheus/secrets/alertmanagers-tls-config/{{$spec.name}}_cert
    {{- end }}
    {{- if $spec.tlsConfig.key }}
    key_file: /etc/prometheus/secrets/alertmanagers-tls-config/{{$spec.name}}_key
    {{- end }}
    {{- if $spec.tlsConfig.skipVerify }}
    insecure_skip_verify: {{ $spec.tlsConfig.insecureSkipVerify }}
    {{- end }}
  {{- end }}
{{- end }}

{{- $context := . }}
{{- if (hasKey .Values.prometheus.internal.alertmanagers "byAddress") }}
  {{- if gt (len .Values.prometheus.internal.alertmanagers.byAddress) 0 }}
apiVersion: v1
kind: Secret
metadata:
  name: alertmanagers-config
  namespace: d8-monitoring
  {{- include "helm_lib_module_labels" (list . (dict "app" "prometheus" "additional-configs-for-prometheus" "main")) | nindent 2 }}
stringData:
  alert-managers.yaml: |
    {{- range .Values.prometheus.internal.alertmanagers.byAddress }}
    {{ include "alert-manager-spec" (list $context .) | nindent 4 }}
    {{- end }}
  {{- end }}
{{- end }}
